package com.Home
{
	import com.*;
	import com.Home.*;
	import com.Engine.*;
	import com.Packet.*;
	import com.UI.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameHomeMain extends JFrame
	{
		//members define
		private var scrollpane:JScrollPane;
		private var panelItems:JPanel;
		
		public function FrameHomeMain()
		{
			super();
			setDragable(false);
			setClosable(false);
			setResizable(false);
			setTitleBar(null);
			setBorder(new EmptyBorder());
			setBackgroundDecorator(new SolidBackground(new ASColor(0,0)));
			
			//component creation
			setSize(new IntDimension(206, 100));
			setPreferredSize(new IntDimension(206, 100));
			var layout0:BoxLayout = new BoxLayout();
			getContentPane().setLayout(layout0);
			
			scrollpane = new JScrollPane();
			scrollpane.setSize(new IntDimension(318, 100));
			
			panelItems = new JPanel();
			panelItems.setLocation(new IntPoint(0, 0));
			panelItems.setSize(new IntDimension(206, 100));
			panelItems.setPreferredSize(new IntDimension(206, 100));
			var layout1:FlowWrapLayout = new FlowWrapLayout();
			layout1.setPreferWidth(200);
			layout1.setAlignment(AsWingConstants.LEFT);
			layout1.setHgap(0);
			layout1.setVgap(0);
			panelItems.setLayout(layout1);
			
			//component layoution
			getContentPane().append(scrollpane);
			
			scrollpane.append(panelItems);
			
			
			//--------------------
			
			scrollpane.getVerticalScrollBar().setVisible(true);
			scrollpane.setVerticalScrollBarPolicy(JScrollPane.SCROLLBAR_ALWAYS);
			scrollpane.setHorizontalScrollBarPolicy(JScrollPane.SCROLLBAR_NEVER);
			scrollpane.setLayout(new ScrollPaneLayout(ScrollPaneLayout.TOP_LEFT));
			this.setClipMasked(false);
			this.getContentPane().setClipMasked(false);
			scrollpane.getVerticalScrollBar().setUIElement(true);
			scrollpane.getVerticalScrollBar().setUI(new MyScrollBarUI);
			
			getContentPane().addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			getContentPane().addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
		}
		
		//private var _filterArray:Array = new Array(new GlowFilter(0xffff00, 1, 20, 20));
		private var background:AssetBackground = null;//new SolidBackground(new ASColor(0x033332,0.8));
		private function onRollOver(event:MouseEvent):void
		{
			if(background == null)
			{
				var myMatrix:Matrix = new Matrix();
				myMatrix.createGradientBox(width, height, 1.6, 50, 50);
				var myColors:Array = [0x033332, 0x033344];
				var myAlphaS:Array = [100, 100];
				var myRalphaS:Array = [0, 225];
				var square:Sprite = new Sprite();
				square.graphics.beginFill(0x033332, 0.8);
				square.graphics.drawRoundRectComplex(0,0,width,height,5,5,5,5);
				square.graphics.endFill();
				background = new AssetBackground(square as DisplayObject);
			}
			setBackgroundDecorator(background);
			this.paintImmediately();
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			setBackgroundDecorator(null);
			this.paintImmediately();
		}
		
		private var _mallItems:Array = new Array();
		public static var _frame:FrameHomeMain = null;
		public static function show():void
		{
			if(_frame == null)
			{
				_frame = new FrameHomeMain();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			_frame.update();
			PanelMainHelp.getInstance().bringToTop(_frame);
			_frame.setLocationXY(4,122);
			_frame.setEnabled(true);
			
		}
		public static function hide():void
		{
			if(isShowing())
			{
				_frame.hide();
				_frame.setEnabled(false);
			}
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		public function update():void
		{
			for each(var obj:Object in _mallItems)
			{
				obj.item = null;
			}
			var nIndex:int = 0;
			var array:Array = Home.buildings;
			var nCount:int = 0;
			for(nIndex = 0; nIndex < array.length; nIndex++)
			{
				var obj_item:Object = array[nIndex];
				if(_mallItems[nIndex] == null)
				{
					_mallItems[nIndex] = new PaneHomeMainItem;
				}
				_mallItems[nIndex].item = obj_item;
				if(int(obj_item.m_nCSVId) != 0)
				{
					if(!panelItems.contains(_mallItems[nIndex]))
						panelItems.append(_mallItems[nIndex]);
					nCount++;
				}
				else
				{
					if(panelItems.contains(_mallItems[nIndex]))
						panelItems.remove(_mallItems[nIndex]);
				}
				//nIndex++
			}
			
			if(nCount > 5)
				panelItems.setPreferredSize(null);
			else
				panelItems.setPreferredSize(new IntDimension(206, 100));
			panelItems.pack();
			panelItems.paintImmediately();
		}
	}
}

import com.*;
import com.Engine.*;
import com.Home.*;
import com.Packet.*;
import com.UI.*;

import flash.display.*;
import flash.events.*;
import flash.external.*;
import flash.filters.*;
import flash.geom.*;
import flash.system.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;

import org.aswing.*;
import org.aswing.border.*;
import org.aswing.border.EmptyBorder;
import org.aswing.colorchooser.*;
import org.aswing.dnd.*;
import org.aswing.event.*;
import org.aswing.ext.*;
import org.aswing.geom.*;
import org.aswing.tree.AbstractLayoutCache;

/**
 * MyPaneHomeMainItem
 */
class PaneHomeMainItem extends JPanel{
	
	//members define
	
	/**
	 * MyPaneHomeMainItem Constructor
	 */
	//members define
	private var btn_name:JLabelButton;
	private var panelState:JPanel;
	private var labelInfo:JLabel;
	
	public function PaneHomeMainItem(){
		//component creation
		setSize(new IntDimension(206, 20));
		setPreferredSize(new IntDimension(206, 20));
		var layout0:FlowWrapLayout = new FlowWrapLayout();
		layout0.setPreferWidth(200);
		layout0.setAlignment(AsWingConstants.LEFT);
		layout0.setHgap(0);
		layout0.setVgap(0);
		setLayout(layout0);
		
		btn_name = new JLabelButton();
		btn_name.setLocation(new IntPoint(5, 5));
		btn_name.setSize(new IntDimension(100, 20));
		btn_name.setPreferredSize(new IntDimension(100, 20));
		btn_name.setText("label");
		
		panelState = new JPanel();
		panelState.setLocation(new IntPoint(82, 2));
		panelState.setSize(new IntDimension(15, 15));
		panelState.setPreferredSize(new IntDimension(15, 15));
		
		labelInfo = new JLabel();
		labelInfo.setLocation(new IntPoint(120, 0));
		labelInfo.setSize(new IntDimension(86, 20));
		labelInfo.setPreferredSize(new IntDimension(86, 20));
		labelInfo.setText("label");
		
		//component layoution
		append(btn_name);
		append(panelState);
		append(labelInfo);
		
		//component layoution
		var filter:GlowFilter = new GlowFilter(0x000000, 1, 2, 2, 10);
		btn_name.setForeground(ASColor.ORANGE);
		btn_name.setFont(new ASFont("宋体", 12, false, false, false, false));
		btn_name.setTextFilters(new Array(filter));
		
		//panelState.wrapSimpleButton(Client.getUIElement("btn_up") as SimpleButton);
		//panelState.setText("");
		
		var font:ASFont = new ASFont("宋体", 12, false, false, false, false);

		labelInfo.setFont(font);
		labelInfo.setForeground(ASColor.GREEN);
		labelInfo.setTextFilters(new Array(filter));
		
		btn_name.addActionListener(onBtnName);

		img_state_normal = new AssetBackground(Client.getUIElement("img_state_normal") as DisplayObject);
		img_state_capture = new AssetBackground(Client.getUIElement("img_state_capture") as DisplayObject);
		img_state_gain = new AssetBackground(Client.getUIElement("img_state_gain") as DisplayObject);
		img_state_levelup = new AssetBackground(Client.getUIElement("img_state_levelup") as DisplayObject);
	}
	private var img_state_normal:AssetBackground = null;
	private var img_state_capture:AssetBackground = null;
	private var img_state_gain:AssetBackground = null;
	private var img_state_levelup:AssetBackground = null;
	private function onBtnName(e:Event):void
	{
		if(Home.isInHome() && !Home.isInSelfHome())
			return;
		PanelMainHelp.tmpMapId = CharInfo.m_nRace;
		PanelMainHelp.tmpNpcId = _item.m_nInnerId;
		var objMapCSV:Object = Client.getInstance().GetMapCSV(PanelMainHelp.tmpMapId);
		if(objMapCSV == null)
		{
			return;
		}
		if(PanelMainHelp.tmpMapId != CharInfo.mapId)
		{
			PanelMainHelp.tmpMapX = int(objMapCSV.base_x);
			PanelMainHelp.tmpMapY = int(objMapCSV.base_y);
			Home.visitHome(CharInfo.charId);
		}
		else
		{
			var npc:ObjectHomeBuilding = SceneMain.getInstance().getObjectByBuildingHomeId(PanelMainHelp.tmpNpcId);
			if(npc != null)
			{
				Home.onClickBuildingHome(npc);
				return;
				if(Point.distance(npc.getPos(),CharInfo.objSelf.getPos()) > 100)
				{
					PanelMainHelp.tmpMapX = int(npc.getPos().x);
					PanelMainHelp.tmpMapY = int(npc.getPos().y);
					Client.getInstance().sendPacketLogic(9,[int(CharInfo.mapId),CharInfo.mapLineId,CharInfo.mapType,CharInfo.mapOwnerId,int(npc.getPos().x),int(npc.getPos().y)]);
					return;
				}
				

				PanelMainHelp.tmpNpcId = -1;
				PanelMainHelp.tmpMapId = -1;
				PanelMainHelp.tmpMapX = 0;
				PanelMainHelp.tmpMapY = 0;
			}
		}
	}
	static public function formatTime(time:int):String
	{
		if(time < 0 )
			time = 0;
		var str:String = new String;
		var hour:int = time / 3600;
		var min:int = (time % 3600)/60;
		var second:int = time%60;
		if(hour >= 0)str += ((hour < 10) ? "0" : "") + hour+':';
		if(min >= 0)str += ((min < 10) ? "0" : "") + min+':';
		str += ((second < 10) ? "0" : "") + second;
		return str;
	}
	
	private var _item:Object = null;
	public function set item(val:Object):void
	{
		_item = val;
		btn_name.setText("");
		//panelState.setText("");
		labelInfo.setText("");
		panelState.setVisible(false);
		panelState.setBackgroundDecorator(null);
		btn_name.setForeground(ASColor.GREEN);
		labelInfo.setForeground(ASColor.GREEN);
		btn_name.setEnabled(!(Home.isInHome() && !Home.isInSelfHome()));
		btn_name.setBackground(!(Home.isInHome() && !Home.isInSelfHome())?ASColor.GRAY:ASColor.WHITE);

		
		if(_item == null)
		{
			return;
		}
		
		var objCSV:Object = Client.getInstance().GetHomeBuildingCSV(_item.m_nCSVId);//m_nCSVId);
		if(objCSV != null)
		{
			btn_name.setText(objCSV.name+' '+_item.m_nLevel+'级');
		}
		panelState.setVisible(true);
		
		if(_item.m_nState == Const.HOME_BUILDING_STATE_CAPTURE)
		{
			labelInfo.setText(_item.m_szCaptureCharName);
			labelInfo.setForeground(ASColor.RED);
			btn_name.setForeground(ASColor.RED);
			btn_name.setBackground(!(Home.isInHome() && !Home.isInSelfHome())?ASColor.GRAY:ASColor.RED);
			panelState.setBackgroundDecorator(img_state_capture);
		}
		if(_item.m_nState == Const.HOME_BUILDING_STATE_LEVELUP)
		{
			btn_name.setForeground(ASColor.GREEN);
			labelInfo.setForeground(ASColor.GREEN);
			labelInfo.setText(formatTime(_item.m_nLevelupTime-_item.m_nLevelupStartTime));
			panelState.setBackgroundDecorator(img_state_levelup);
			
		}
		if(_item.m_nState == Const.HOME_BUILDING_STATE_NORMAL)
		{
			labelInfo.setForeground(ASColor.WHITE);
			btn_name.setForeground(ASColor.WHITE);
			var time:int = _item.m_nGainTime-_item.m_nGainStartTime;
			if(time <= 0)
			{
				labelInfo.setForeground(ASColor.ORANGE);
				btn_name.setForeground(ASColor.ORANGE);
				panelState.setBackgroundDecorator(img_state_gain);
				labelInfo.setText("可收获");
			}
			else
			{
				labelInfo.setText(formatTime(_item.m_nGainTime-_item.m_nGainStartTime));
				panelState.setBackgroundDecorator(img_state_normal);
			}
			
		}


		this.pack();
		this.paintImmediately();
	}
	
	public function get item():Object
	{
		return _item;
	}
	//_________getters_________
	
	
}